package switchviz.gui.component;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import switchviz.core.ResourceManager;


public class JLabeldComponent extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8609811128401172855L;
	private JComponent _component = null;
	private JLabel _label = null;
	private int _type;
	
	public static final int TEXTFIELD = 1;
	public static final int TEXTAREA = 2;
	public static final int COMBOBOX = 3;
	public static final int CHECKBOX = 4;
	public static final int LABEL = 5;
	
	public JLabeldComponent(String labeltext, int type)
	{
		this(labeltext, type, ResourceManager.getInstance().getConfigurationLabelDimension());
	}
	
	public JLabeldComponent(String labeltext, int type, Dimension dimension)
	{
		_label =  new JLabel(labeltext, JLabel.TRAILING);
		_label.setPreferredSize(dimension);
		_type = type;
		
		this.setLayout(new BorderLayout());
		this.add(_label, BorderLayout.WEST);
		
		switch(_type)
		{
		case TEXTFIELD:
			_component = new JTextField();
			_component.setSize(new Dimension(250, 20));
			break;
		case TEXTAREA:
			_component = new JTextArea();
			((JTextArea) _component).setColumns(15);
	        ((JTextArea) _component).setRows(10);
			((JTextArea) _component).setWrapStyleWord(true);
			((JTextArea) _component).setLineWrap(true);
			((JTextArea) _component).setMargin(new Insets(1, 4, 1, 4));
			JScrollPane scrollpane = new JScrollPane(_component);
			this.setMaximumSize(new Dimension(800, 180));
			this.add(scrollpane, BorderLayout.CENTER);
			return;
		case COMBOBOX:
			_component = new JComboBox();
			break;
		case CHECKBOX:
			_component = new JCheckBox();
			break;
		case LABEL:
			_component = new JLabel();
			_component.setSize(new Dimension(150, 20));
		}
		this.setMaximumSize(new Dimension(800, 20));
		this.add(_component, BorderLayout.CENTER);
	}
	
	public JTextField getTextField()
	{
		return (JTextField)_component;
	}
	
	public JFormattedTextField getFormattedTextField()
	{
		return (JFormattedTextField)_component;
	}
	
	public JTextArea getTextArea()
	{
		return (JTextArea)_component;
	}
	
	public JComboBox getComboBox()
	{
		return (JComboBox)_component;
	}
	
	public JCheckBox getCheckBox()
	{
		return (JCheckBox)_component;
	}
	
	public JLabel getLabel2()
	{
		return (JLabel) _component;
	}
	
	public JComponent getComponent()
	{
		return _component;
	}
	
	public JLabel getLabel()
	{
		return _label;
	}
	
	public String getText()
	{
		switch(_type)
		{
		case TEXTFIELD:
			return ((JTextField)_component).getText();
		case TEXTAREA:
			return ((JTextArea)_component).getText();
		case COMBOBOX:
			if(((JComboBox)_component).getItemCount() > 0)
				return ((JComboBox)_component).getSelectedItem().toString();
			else
				return "";
		case CHECKBOX:
			return ((JCheckBox)_component).getText();
		case LABEL:
			return ((JLabel)_component).getText();
		default:
			return "";
		}
	}
	
	public void setText(String text)
	{
		switch(_type)
		{
		case TEXTFIELD:
			((JTextField)_component).setText(text);
			break;
		case TEXTAREA:
			((JTextArea)_component).setText(text);
			break;
		case COMBOBOX:
			((JComboBox)_component).setSelectedItem(text);
			break;
		case CHECKBOX:
			((JCheckBox)_component).setText(text);
			break;
		case LABEL:
			((JLabel)_component).setText(text);
		}
	}
}
